package sh.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.opensymphony.xwork2.ActionSupport;

import sh.service.UploadService;
import sh.service.impl.UploadServiceImpl;

public class TestUpLoad extends ActionSupport {
	private File upFile;
	private String upFileFileName;
	private String upFileContentType;
	//jsp中需要查看的图片文件
	private File downFile;
	
	//测试将图片放入到action中，保存到硬盘
	public String showImage() throws IOException{
		File file = new File("c:\\blog.jpg");
		File file2 = new File("c:\\2.jpg");
		this.setDownFile(file);
		
		
		
		FileOutputStream fos = new FileOutputStream(file2);
		//InputStream is = new InputStream();
		FileInputStream fis = new FileInputStream(file);
		
		byte[] Buffer = new byte[4096];
		
		int size = 0;
		   
	    while ((size = fis.read(Buffer)) != -1) {
	     // System.out.println(size);
	     fos.write(Buffer, 0, size);
	    }
		System.out.println(downFile.length()+ "的文件长度");
		return "success";
	}
	
	//将上传的图片保存到硬盘
	public String save() throws IOException{
		//File file = new File("c:\\blog.jpg");
		File file2 = new File("c:\\3.jpg");
		//this.setDownFile(upFile);
		
		
		
		FileOutputStream fos = new FileOutputStream(file2);
		//InputStream is = new InputStream();
		FileInputStream fis = new FileInputStream(upFile);
		
		byte[] Buffer = new byte[4096];
		
		int size = 0;
		   
	    while ((size = fis.read(Buffer)) != -1) {
	     // System.out.println(size);
	     fos.write(Buffer, 0, size);
	    }
		System.out.println(downFile.length()+ "的文件长度");
		return "success";
	}
	
	//上传图片
	@Override
	public String execute() throws Exception {
		UploadService test = new UploadServiceImpl();
		if(test.add(upFile)){
		return super.execute();
		}else{
			return "fail";
		}
		//return "success";
	}

	public File getUpFile() {
		return upFile;
	}
	
	public void setUpFile(File upFile) {
		this.upFile = upFile;
		System.out.println(this.upFile.length()+ "的长度");
		//System.out.println("得到上传文件2");
		System.out.println(this.upFile.length()+ "的长度");
		System.out.println("得到上传文件");
	}

	public String getUpFileFileName() {
		return upFileFileName;
	}

	public void setUpFileFileName(String upFileFileName) {
		this.upFileFileName = upFileFileName;
	}

	public String getUpFileContentType() {
		return upFileContentType;
	}

	public void setUpFileContentType(String upFileContentType) {
		this.upFileContentType = upFileContentType;
	}

	public void setDownFile(File downFile) {
		this.downFile = downFile;
	}

	public File getDownFile() {
		return downFile;
	}
}
